package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.QiyeDAO;
import com.modal.CompanyModel;
import com.pojo.Dictionary;
import com.pojo.Qiye;
import com.util.StringUtil;
import com.vo.QiyeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: T-bao
 * @Date: 2021-3-26 14:39
 * @Description:
 */
@Service
public class QiyeService extends ServiceImpl<QiyeDAO, Qiye> {

    @Autowired
    private QiyeDAO qiyeDAO;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 查询未审核企业
     * @param qyname    企业名称
     * @param page      页数
     * @param limit     条数
     * @return
     */
    public IPage<QiyeVO> shenheQiye(String qyname, int page, int limit){
        return qiyeDAO.shenheQiye(new Page(page,limit),qyname);
    }

    /**
     * 查询未审核企业数量
     * @param qyname    企业名称
     * @return
     */
    public int shenhecount(String qyname){
        return qiyeDAO.shenhecount(qyname);
    }

    /**
     * 查找状态为启用已审核的企业
     * @return
     */
    public List<Qiye> QiyeStatus(){
        List<Qiye> qiye = qiyeDAO.selectList(new QueryWrapper<Qiye>().eq("status" , 1).eq("shstatus",1)) ;
        return qiye;
    }

    /**
     * 根据企业ID查询企业
     * @param id    企业ID
     * @return
     */
    public Qiye selectID(String id){
        return qiyeDAO.selectById(id);
    }

    /**
     * 分页查询企业
     * @param qyname    企业名称
     * @param page      页数
     * @param limit     条数
     * @param wyid      物业ID
     * @param status    企业状态
     * @return
     */
    public List<QiyeVO> selectQiye(String qyname, int page, int limit,String wyid, Integer status){
        return qiyeDAO.selectQiye(qyname,page,limit , wyid, status);
    }

    /**
     * 根据企业名称和物业ID查询企业数量
     * @param qyname    企业名称
     * @param id        物业ID
     * @return
     */
    public int count(String qyname,String id, Integer status){
        return qiyeDAO.count(qyname,id, status);
    }

    /**
     * 根据企业名称和企业ID查询企业数量
     * @param qyname    企业名称
     * @param id        企业ID
     * @return
     */
    public int countNameID(String qyname,String id){
        return qiyeDAO.countNameID(qyname,id);
    }

    /**
     * 根据企业名称查询企业数量
     * @param qyname    企业名称
     * @return
     */
    public int countName(String qyname){
        return qiyeDAO.countName(qyname);
    }

    /**
     * 根据企业ID修改企业状态
     * @param id    企业ID
     * @return
     */
    public Qiye upshStatus(String id){
        Qiye theme = qiyeDAO.selectById(id);
        if (theme==null)
            return theme;

        Integer oldStatus = theme.getShstatus();
        if (oldStatus==null)
            oldStatus = 0 ;

        theme.setShstatus(1-oldStatus);

        qiyeDAO.updateById(theme);
        return theme;
    }

    /**
     * 根据物业ID查询企业
     * @param wuye  物业ID
     * @return
     */
    public List<Qiye> selectQiyes(String wuye) {
        QueryWrapper<Qiye> query = new QueryWrapper<Qiye>().eq("status", 1).eq("shstatus",1);
        if(StringUtil.isNotNull(wuye)) {
            query.eq("wyID" , wuye) ;
        }
        return qiyeDAO.selectList(query) ;
    }

    /**
     * 物业登录查询启用已审核企业
     * @param id 物业ID
     * @return
     */
    public List<Qiye> wyQiyeStatus(String id) {
        List<Qiye> qiye = qiyeDAO.selectList(new QueryWrapper<Qiye>().eq("status" , 1).eq("wyId" , id).eq("shstatus",1)) ;
        return qiye;
    }

    /**
     * 物业登录查询企业
     * @param qyname    企业名称
     * @param page      页数
     * @param limit     条数
     * @param id        物业ID
     * @return
     */
    public List<QiyeVO> wySelectQiye(String qyname, int page, int limit, String id, Integer status) {
        return qiyeDAO.selectQiye(qyname,page,limit , id, status);
    }

    /**
     * 查询已到期企业 分页
     * @param qyname 企业名称
     * @param page
     * @param limit
     * @return
     */
    public IPage<Qiye> selectYidaoqi(String qyname, int page, int limit){
        Page p = new Page(page,limit);
        QueryWrapper query = new QueryWrapper();
        if(StringUtil.isNotNull(qyname)){
            query.like("qyname",qyname);
        }
        query.eq("shstatus",1);
        query.le("qyDqtime",new Date());

        IPage<Qiye> iPage = qiyeDAO.selectPage(p,query);
        return iPage;
    }

    /**
     * 查询已到期企业数量
     * @return
     */
    public Long selectYidaoqiCount(){
        return qiyeDAO.selectCount(new QueryWrapper<Qiye>().le("qyDqtime",new Date()).eq("shstatus",1));
    }

    /**
     * 根据企业名称查询启用企业
     * @param name  企业名称
     * @return
     */
    public Qiye getQiyeByName (String name) {
        QueryWrapper query = new QueryWrapper();
        query.eq("qyname",name);
        query.eq("status",1);
        List<Qiye> qiye = qiyeDAO.selectList(query);
        if (qiye.size() > 0) {
            return  qiye.get(0);
        }
        return null;
    }

    /**
     * 根据企业编码和企业ID查询企业
     * @param qyCode    企业编码
     * @param id        企业ID
     * @return
     */
    public int selectQyCode(String qyCode,String id){
        QueryWrapper query = new QueryWrapper<Qiye>();
        query.eq("qycode",qyCode);
        if (StringUtil.isNotNull(id)) {
            query.eq("id",id);
        }
        List<Qiye> qyList = qiyeDAO.selectList(query);
        return qyList.size();
    }

    /**
     * 根据企业名称模糊查询企业
     * @param name
     * @return
     */
    public List<Qiye> getQiyeListByName (String name) {
        QueryWrapper query = new QueryWrapper();
        query.like("qyname", name);
        query.eq("status", 1);
        return qiyeDAO.selectList(query);
    }

    /**
     * 查询即将到期企业 分页
     * @param qyname 企业名称
     * @param page
     * @param limit
     * @return
     */
    public IPage<Qiye> selectJiJiangDaoQi(String qyname, int page, int limit){
        Page p = new Page(page,limit);
        QueryWrapper query = new QueryWrapper();
        Dictionary dictionary = dictionaryService.getDicByCode("JJDQQY", "JJDQQY");
        Calendar calc = Calendar.getInstance();
        calc.add(calc.DAY_OF_MONTH, Integer.valueOf(dictionary.getValue()));
        String currDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calc.getTime());
        if(StringUtil.isNotNull(qyname)){
            query.like("qyname",qyname);
        }
        query.eq("shstatus",1);
        query.ge("qyDqtime",new Date());
        query.le("qyDqtime",currDate);

        IPage<Qiye> iPage = qiyeDAO.selectPage(p,query);
        return iPage;
    }

    /**
     * 获取所有企业
     * @param wuye
     * @return
     */
    public List<Qiye> getAllQiye(String wuye) {
        QueryWrapper<Qiye> query = new QueryWrapper();
        if(StringUtil.isNotNull(wuye)) {
            query.eq("wyID" , wuye) ;
        }
        return qiyeDAO.selectList(query) ;
    }

//    public String insertQiye(CompanyModel model){
//        Qiye qiye = new Qiye();
//        qiye.setId(model.getId());
//        qiye.setAddress(model.getAddress());
//        qiye.setCreateTime(model.getCreatetime());
//        qiye.setMessage(model.getMessage());
//        qiye.setStatus(model.getStatus());
//        qiye.setQyname(model.getQyname());
//        qiye.setWyID(model.getWyID());
//        qiye.setShstatus(model.getShstatus());
//        if (qiyeDAO.insert(qiye) < 1) {
//            return "新增失败";
//        }
//        return null;
//    }

    public String updateQiye(CompanyModel model){
        Qiye qiye = qiyeDAO.selectById(model.getId());
        if (qiye == null) {
            return "公司不存在";
        }
        if (StringUtil.isNotNull(model.getQyname())) {
            qiye.setQyname(model.getQyname());
        }
        if (StringUtil.isNotNull(model.getAddress())) {
            qiye.setAddress(model.getAddress());
        }
        if (StringUtil.isNotNull(model.getQyname())) {
            qiye.setQyname(model.getQyname());
        }
        if (StringUtil.isNotNull(model.getMessage())) {
            qiye.setMessage(model.getMessage());
        }
        if (StringUtil.isNotNull(model.getQyname())) {
            qiye.setQyname(model.getQyname());
        }
        if (StringUtil.isNotNull(model.getWyID())) {
            qiye.setWyID(model.getWyID());
        }
        qiye.setStatus(model.getStatus());
        qiye.setShstatus(model.getShstatus());
        if (qiyeDAO.updateById(qiye) < 1) {
            return "修改失败";
        }
        return null;
    }
}
